package com.cmwa.solution.business.investTrade.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cmwa.ec.base.util.StringUtil;
import com.cmwa.solution.base.controller.BaseController;
import com.cmwa.solution.base.entity.Employee;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.business.investTrade.entity.AsseTradeRelVo;
import com.cmwa.solution.business.investTrade.entity.AsseUnitVo;
import com.cmwa.solution.business.investTrade.entity.TradeUnitVo;
import com.cmwa.solution.business.investTrade.service.TradeStructureService;
import com.cmwa.solution.utils.SessionUtils;

import net.sf.json.JSONObject;

/**
 * 交易结构控制器
 * @author ex-chenbq
 *
 */
@Controller
@RequestMapping("/business/tradeStructure")
public class TradeStructureController extends BaseController {

	private final static String JSP_PATH = "business/investTrade/";
	@Resource
	private TradeStructureService tradeStructureService;
		
	/**
	 * 交易结构列表页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/tradeStructureListView")
	public String tradeStructureListView(ModelMap model) {
		return JSP_PATH + "tradeStructureList";
	}
	
	/**
	 * 资产单元新增页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/asseUnitAddView")
	public ModelAndView asseUnitAddView() {
		ModelAndView model = new ModelAndView();
		model.addObject("method","add");
		model.setViewName(JSP_PATH + "asseUnitAdd");
		return model;
	}
	
	/**
	 * 资产单元详情页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/asseUnitDetailView")
	public ModelAndView asseUnitDetailView(@RequestParam("id") String id ,@RequestParam("type") String type ) {
		ModelAndView model = new ModelAndView();
		AsseUnitVo asseUnitVo = tradeStructureService.queryAsseUnitById(id);
		if (null == asseUnitVo) {
			model.addObject("exception", "资产单元信息不存在");
			model.setViewName("system/hint/validateFailed");
			return model;
		}
		model.addObject("vo", asseUnitVo);
		model.addObject("method",type);
		model.setViewName(JSP_PATH + "asseUnitAdd");
		return model;
	}
	
	/**
	 * 交易单元新增页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/tradeUnitAddView")
	public String tradeUnitAddView(ModelMap model) {
		return JSP_PATH + "tradeUnitAdd";
	}
	
	/**
	 * 交易资产关联页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/asseAndtradeUnitRelAddView")
	public ModelAndView asseAndtradeUnitRelAddView(ModelAndView model) {
		SearchParam param = new SearchParam();
		List<AsseUnitVo> list = tradeStructureService.queryAsseUnitList(param );
		model.addObject("asseUnitList",list);
		model.setViewName(JSP_PATH + "asseAndtradeUnitRelAdd");
		return model;
	}
	
	/**
	 * 分页查询资产单元数据
	 * @param param
	 * @return
	 */
	@RequestMapping("/queryAsseUnitListPage")
	@ResponseBody
	public Page<AsseUnitVo> queryAsseUnitListPage(SearchParam param){
		if (StringUtils.equals(SessionUtils.getEmployee().getEmpType(), "OUT")) {
			param.getSp().put("empId", SessionUtils.getEmployee().getID());
		}
		Page<AsseUnitVo> list = tradeStructureService.queryAsseUnitListPage(param);
		return list;
	}
	
	/**
	 * 分页查询交易单元数据
	 * @param param
	 * @return
	 */
	@RequestMapping("/queryTradeUnitListPage")
	@ResponseBody
	public Page<TradeUnitVo> queryTradeUnitListPage(SearchParam param){
		if (StringUtils.equals(SessionUtils.getEmployee().getEmpType(), "OUT")) {
			param.getSp().put("empId", SessionUtils.getEmployee().getID());
		}
		Page<TradeUnitVo> list = tradeStructureService.queryTradeUnitListPage(param);
		return list;
	}
	
	/**
	 * 查询所有交易单元数据
	 * @param param
	 * @return
	 */
	@RequestMapping("/queryTradeUnitList")
	@ResponseBody
	public List<TradeUnitVo> queryTradeUnitList(SearchParam param){
		if (StringUtils.equals(SessionUtils.getEmployee().getEmpType(), "OUT")) {
			param.getSp().put("empId", SessionUtils.getEmployee().getID());
		}
		List<TradeUnitVo> list = tradeStructureService.queryTradeUnitList(param);
		return list;
	}
	
	/**
	 * 根据资产单元ID查询交易资产关联关系
	 * @param assetUnitId
	 * @return
	 */
	@RequestMapping("/queryAsseAndTradeUnitRelById")
	@ResponseBody
	public List<AsseTradeRelVo> queryAsseAndTradeUnitRelById(@RequestParam("assetUnitId")String assetUnitId){
		List<AsseTradeRelVo> list = tradeStructureService.queryAsseAndTradeUnitRelById(assetUnitId);
		return list;
	}
	
	/**
	 * 新增资产单元
	 * @param asseUnitVo
	 * @return
	 */
	@RequestMapping("/insertAsseUnit")
	public ModelAndView insertAsseUnit(AsseUnitVo asseUnitVo) {
		ModelAndView model = new ModelAndView();
		Employee employee = SessionUtils.getEmployee();
		try {
			asseUnitVo.setCreateId(employee.getID());
			asseUnitVo.setModifyId(employee.getID());
			tradeStructureService.insertAsseUnit(asseUnitVo);
			model.setViewName("system/hint/success");
		} catch (Exception e) {
			e.printStackTrace();
			model.addObject("exception", e.getMessage());
			model.setViewName("system/hint/validateFailed");
		}
		return model;
	}
	
	/**
	 * 修改资产单元信息
	 * @param asseUnitVo
	 * @return
	 */
	@RequestMapping("/updateAsseUnit")
	public ModelAndView updateAsseUnit(AsseUnitVo asseUnitVo) {
		ModelAndView model = new ModelAndView();
		Employee employee = SessionUtils.getEmployee();
		try {
			asseUnitVo.setModifyId(employee.getID());
			tradeStructureService.updateAsseUnit(asseUnitVo);
			model.setViewName("system/hint/success");
		} catch (Exception e) {
			e.printStackTrace();
			model.addObject("exception", e.getMessage());
			model.setViewName("system/hint/validateFailed");
		}
		return model;
	}
	

	/**
	 * 删除资产单元信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteAsseUnit")
	@ResponseBody
	public JSONObject deleteAsseUnit(@RequestParam("id") String id) {
		JSONObject jsonObject = new JSONObject();
		try {
			tradeStructureService.deleteAsseUnit(id);
			tradeStructureService.deleteAsseAndTradeUnitRel(id);
			jsonObject.put("resultCode", "0000");
			jsonObject.put("resultMsg", "资产单元信息删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			jsonObject.put("resultCode", "9999");
			jsonObject.put("resultMsg", "资产单元信息删除失败："+e.getMessage());
		}
		return jsonObject;
	}
	
	/**
	 * 新增交易单元
	 * @param tradeUnitVo
	 * @return
	 */
	@RequestMapping("/insertTradeUnit")
	public ModelAndView insertTradeUnit(TradeUnitVo tradeUnitVo) {
		ModelAndView model = new ModelAndView();
		Employee employee = SessionUtils.getEmployee();
		try {
			tradeUnitVo.setCreateId(employee.getID());
			tradeUnitVo.setModifyId(employee.getID());
			tradeStructureService.insertTradeUnit(tradeUnitVo);
			model.setViewName("system/hint/success");
		} catch (Exception e) {
			e.printStackTrace();
			model.addObject("exception", e.getMessage());
			model.setViewName("system/hint/validateFailed");
		}
		return model;
	}
	
	/**
	 * 新增交易单元
	 * @param tradeUnitVo
	 * @return
	 */
	@RequestMapping("/insertAsseAndTradeUnitRel")
	public ModelAndView insertAsseAndTradeUnitRel(AsseTradeRelVo asseTradeRelVo) {
		ModelAndView model = new ModelAndView();
		Employee employee = SessionUtils.getEmployee();
		try {
			tradeStructureService.deleteAsseAndTradeUnitRel(asseTradeRelVo.getAssetUnitId());
			if (!StringUtil.isEmpty(asseTradeRelVo.getTradeUnitId())) {
				String ids[] = asseTradeRelVo.getTradeUnitId().split(",");
				for (int i = 0; i < ids.length; i++) {
					AsseTradeRelVo tradeRelVo = new AsseTradeRelVo();
					tradeRelVo.setAssetUnitId(asseTradeRelVo.getAssetUnitId());
					tradeRelVo.setTradeUnitId(ids[i]);
					tradeRelVo.setCreateId(employee.getID());
					tradeRelVo.setModifyId(employee.getID());
					tradeStructureService.insertAsseAndTradeUnitRel(tradeRelVo );
				}
			}
			model.setViewName("system/hint/success");
		} catch (Exception e) {
			e.printStackTrace();
			model.addObject("exception", e.getMessage());
			model.setViewName("system/hint/validateFailed");
		}
		return model;
	}
}

